package com.shanghaionstar.discover.charge.service;

import android.content.Context;

import com.google.gson.Gson;
import com.onstar.cn.api.UsersService;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.mvp.base.core.BaseService;
import com.onstar.cn.framework.network.HttpInjectUtils;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.shanghaionstar.discover.charge.model.ChargeStateReq;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.FinalConstants;
import com.shanghaionstar.utils.NetConnectUtil;

import java.util.LinkedHashMap;

import rx.Observable;
import rx.functions.Func0;

/**
 * Created by Rengui Lu on 2016/7/28.
 */
public class ChargeNotificationService extends BaseService<Class<?>> {
    private Context mContext;

    public ChargeNotificationService(Context context) {
        this.mContext =  context;
    }

    @Override
    public RequestEntity getRequestEntity() {
        return null;
    }

    @Override
    public Class<?> jsonToBean(String json) {
        return null;
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO) , method = HttpMethod.GET)
    public Observable<String> loadChargeState() {
        return Observable.defer(new Func0<Observable<String>>() {
            @Override
            public Observable<String> call() {
                try {
                    UsersService service = new UsersService(Constants.SERVER_URL);
                    RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(mContext);
                    String ipdID = ApplicationUtil.getInstance().getIdpID();
                    String accountID = ApplicationUtil.getInstance().getAccountId();
                    String vin = ApplicationUtil.getInstance().getVin();
                    service.getChargeState(requestEntity, ipdID, accountID, vin);
                    requestEntity = HttpInjectUtils.injectHttp(ChargeNotificationService.this.getClass(), "loadChargeState", requestEntity);
                    return Observable.just(BusinessUtil.getResponse(getDatas(requestEntity), mContext));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO) , method = HttpMethod.PUT)
    public Observable<String> putChargeState(final boolean isClose) {
        return Observable.defer(new Func0<Observable<String>>() {
            @Override
            public Observable<String> call() {
                try {
                    UsersService service = new UsersService(Constants.SERVER_URL);
                    RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(mContext);
                    LinkedHashMap<String, Object> coHashMap = new LinkedHashMap<String, Object>();
                    coHashMap.put(FinalConstants.HTTP_AUTHORIZATION, ApplicationUtil.getInstance().getToken());
                    requestEntity.setCustomHeads(coHashMap);
                    String ipdID = ApplicationUtil.getInstance().getIdpID();
                    String accountID = ApplicationUtil.getInstance().getAccountId();
                    String vin = ApplicationUtil.getInstance().getVin();
                    service.putChargeState(requestEntity, ipdID, accountID, vin);
                    ChargeStateReq res = new ChargeStateReq();
                    res.setChargeSwitch(isClose);
                    requestEntity.setBody(new Gson().toJson(res));
                    requestEntity = HttpInjectUtils.injectHttp(ChargeNotificationService.this.getClass(), "putChargeState", requestEntity, Boolean.class);
                    return Observable.just(BusinessUtil.getResponse(getDatas(requestEntity), mContext));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }


}
